package cn.edu.sgu.www.mhxysy.service.chongwu.impl;

import cn.edu.sgu.www.mhxysy.dto.chongwu.ChongwuAttributeDTO;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuAttribute;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuJiadian;
import cn.edu.sgu.www.mhxysy.enums.OpenStatus;
import cn.edu.sgu.www.mhxysy.mapper.chongwu.ChongwuAttributeMapper;
import cn.edu.sgu.www.mhxysy.mapper.chongwu.ChongwuJiadianMapper;
import cn.edu.sgu.www.mhxysy.service.chongwu.ChongwuAttributeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ChongwuAttributeServiceImpl implements ChongwuAttributeService {

	private final ChongwuJiadianMapper chongwuJiadianMapper;
	private final ChongwuAttributeMapper chongwuAttributeMapper;

	@Autowired
	public ChongwuAttributeServiceImpl(
			ChongwuJiadianMapper chongwuJiadianMapper,
			ChongwuAttributeMapper chongwuAttributeMapper) {
		this.chongwuJiadianMapper = chongwuJiadianMapper;
		this.chongwuAttributeMapper = chongwuAttributeMapper;
	}

	@Override
	public void updateById(ChongwuAttributeDTO chongwuAttributeDTO) {
		ChongwuAttribute chongwuAttribute = toEntity(chongwuAttributeDTO);

		chongwuAttributeMapper.updateById(chongwuAttribute);
	}

	@Override
	public ChongwuAttribute selectById(String id) {
		return chongwuAttributeMapper.selectById(id);
	}

	@Override
	public ChongwuAttribute selectByChongwuId(String chongwuId) {
		// 查询宠物开启的加点
		QueryWrapper<ChongwuJiadian> queryWrapper = new QueryWrapper<>();

		queryWrapper.eq("chongwu_id", chongwuId);
		queryWrapper.eq("open_status", OpenStatus.OPEN.getValue());

		ChongwuJiadian chongwuJiadian = chongwuJiadianMapper.selectOne(queryWrapper);

		if (chongwuJiadian != null) {
			// 通过宠物ID和宠物加点ID查询宠物属性
			QueryWrapper<ChongwuAttribute> querywrapper = new QueryWrapper<>();

			querywrapper.eq("chongwu_id", chongwuId);
			querywrapper.eq("jiadian_id", chongwuJiadian.getId());

			return chongwuAttributeMapper.selectOne(querywrapper);
		}

		return null;
	}

	private ChongwuAttribute toEntity(ChongwuAttributeDTO chongwuAttributeDTO) {
		ChongwuAttribute chongwuAttribute = new ChongwuAttribute();

		chongwuAttribute.setId(chongwuAttributeDTO.getId());
		chongwuAttribute.setQixue(chongwuAttributeDTO.getQixue());
		chongwuAttribute.setMofa(chongwuAttributeDTO.getMofa());
		chongwuAttribute.setExp(chongwuAttributeDTO.getExp());
		chongwuAttribute.setWlsh(chongwuAttributeDTO.getWlsh());
		chongwuAttribute.setFssh(chongwuAttributeDTO.getFssh());
		chongwuAttribute.setWlfy(chongwuAttributeDTO.getWlfy());
		chongwuAttribute.setFsfy(chongwuAttributeDTO.getFsfy());
		chongwuAttribute.setSudu(chongwuAttributeDTO.getSudu());

		return chongwuAttribute;
	}

}